<?php

class Controller_Blog extends Controller_Website
{

    public function before() {
        parent::before();
        $this->section = 'blog';
        $this->template->activeCat = 'blog';
        $this->itemsPerPage = 6;
        $this->commentsPerPage = 5;
        $this->addScript('/scripts/autoScroll.js');
        $this->addScript('/scripts/jquery.placeholder.min.js');
    }

	public function actionIndex($alias = null, $page = 1, $paginator = null)
	{
        $page = (int)$page - 1;
        if(Request::$isAjax && $alias == 'noalias'){
            $this->autoRender = false;

            $posts = Model_Directory::getItemsWithLimit($this->section,'position','ASC',$page,$this->itemsPerPage);


            foreach ($posts as $post) {
                echo'
                    <div class="blog_item">
                        <div class="blog_item_left">
                            <div class="blog_image">
                                <a href="'.Request::$controller.'index/'.$post['alias'].'/"> <img src="'.Model_Directory::itemImagePath($post,'blogMini').'" alt=""></a>
                            </div>
                        </div>
                        <div class="blog_item_right">
                            <div class="blog_item_header">
                                <div class="blog_item_header_left"><a href="'.Request::$controller.'index/'.$post['alias'].'/">'.$post['name'].'</a></div>
                                <div class="blog_item_header_right">'.$post['date'].'</div>
                            </div>
                            <div class="blog_item_text">
                                '.$post['text1'].'
                            </div>
                            <div class="blog_item_bottom">
                                <div class="blog_item_bottom_left">
                                    <a href="<?=Request::$controller?>index/'.$post['alias'].'/"></a>
                                </div>
                                <div class="blog_item_bottom_right">
                                    <span class="blog_views_icon"></span>
                                    <span class="blog_views_count">'.Model_Manager::getViews($post['id']).'</span>
                                    <span class="blog_comments_icon"></span>
                                    <span class="blog_comments_count">'.count(Model_Manager::getCommentsByPostId($post['id'])).'</span>
                                </div>

                            </div>
                        </div>
                    </div>

                ';
            }


            exit();
        }


        $this->template->title = $this->settings['title'];

//        system::consoleDump();

        if(isset($alias)){

            $post = $this->direc->getItemByAlias($alias, $this->section);
            $postId = $post['id'];

            //AJAX AUTOLOADING ON SCROLL
        if(Request::$isAjax){
            $this->autoRender = false;


            $comments = Model_Manager::getCommentsByPostIdWithLimit($postId,'id','DESC',$page,$this->commentsPerPage);


            if($comments){
                foreach($comments as $comment){
                    if($comment['hasReplies'] == 1){
                        $commentIds[] = $comment['id'];
                    }
                }

                if(!empty($commentIds)){
                    $commIdsImploded = implode($commentIds,',');
                    $replies = Model_Manager::getReplies($commIdsImploded);
                }
            }

            //print all ajax response here
            if(!empty($comments)){
                foreach ($comments as $comment) {
                    if($comment['isReply'] == 0){
print <<<HERE
                        <div class="full_blog_comment" data-id="{$comment['id']}">
                                <div class="full_blog_comment_header">
                                    <div class="full_blog_comment_header_left">
                                        <div class="comment_author">{$comment['author']}</div>
                                        <div class="comment_date">{$comment['time']}</div>
                                        <div class="clear"></div>
                                    </div>
                                    <div class="full_blog_comment_header_right">
                                        <div class="comment_controls">
HERE;


                                            if(isset($_SESSION['identity']) && $comment['author_id']==$_SESSION['identity']['id']){
                                                echo '<span class="comment_delete"></span>';
                                            }
print <<<HERE
                                            <span class="comment_reply"></span>
                                        </div>
                                    </div>
                                    <div class="clear"></div>
                                </div>

                                <div class="comment_text">
                                    {$comment['text']}
                                </div>

                            </div>

HERE;


                    }
                    //reply printing

                    if($comment['hasReplies'] == 1){
                        foreach ($replies  as $reply){
                            if($reply['parent_id'] == $comment['id']){
print <<<HERE
                                <div class="full_blog_comment_reply">
                                        <div class="full_blog_comment_header">
                                            <div class="full_blog_comment_header_left">
                                                <div class="comment_author">{$reply['author']}</div>
                                                <div class="comment_date">{$reply['time']}</div>
                                                <div class="clear"></div>
                                            </div>
                                            <div class="clear"></div>
                                        </div>

                                        <div class="comment_text">
                                            {$reply['text']}
                                        </div>
                                    </div>
HERE;

                            }
                        }
                    }


                }

            }


            //end here

            exit();
        }
        //view blog item
            $this->template->content = new View('blog/post');

            $this->template->content->post = $post;

            //add view +1
            $this->manager->addView($alias);

//            $comments = $this->manager->getCommentsByPostId($postId);
            $comments = Model_Manager::getCommentsByPostIdWithLimit($postId,'id','DESC',$page,$this->commentsPerPage);

            if($comments){
                foreach($comments as $comment){
                    if($comment['hasReplies'] == 1){
                        $commentIds[] = $comment['id'];
                    }
                }

                if(!empty($commentIds)){
                    $commIdsImploded = implode($commentIds,',');
                    $replies = Model_Manager::getReplies($commIdsImploded);
                    $this->template->content->replies = $replies;
                }

                $this->template->content->comments = $comments;
            }

        }
        else{ //view blog list


            $this->template->content = new View('blog/index');

            //all posts
//            $posts = $this->direc->getItems($this->section, $order = 'position', $orderDir = 'ASC');
            $posts = Model_Directory::getItemsWithLimit($this->section,'position','ASC',$page,$this->itemsPerPage);
            $this->template->content->posts = $posts;
        }

    }

    public function actionAddcomment(){
        if(Request::$isAjax){
            $this->autoRender = false;

            $comment = Html::chars($_POST['comment']);

            $postId = $_POST['postId'];

            $author = isset($_SESSION['identity'])? $_SESSION['identity']['name'] : $_POST['author'];
            $author_id = isset($_SESSION['identity'])? $_SESSION['identity']['id'] : '';

            if($insertId = Model_Manager::addComment($author,$author_id,$comment,$postId)){
                $response['success']['message'] = 'Your comment has been added';
//                $response['success']['insertId'] = $insertId;
                $insertedComment = Model_Manager::getCommentById($insertId);
                $response['success']['comment'] = $insertedComment;

                if(!isset($_SESSION['identity'])){
                    $response['success']['deletion'] = false;
                }
            }
            else{
                $response['error'] = 'An error has been occured.Please try again later';
            }

            $this->response->body = json_encode($response);
        }
    }

    public function actionAddreply(){
        if(Request::$isAjax){
            $this->autoRender = false;

            $comment = HTML::chars($_POST['comment']);
            $postId = $_POST['postId'];
            $parentId = $_POST['parentId'];

            $author = isset($_SESSION['identity'])? $_SESSION['identity']['name'] : $_POST['author'];
            $author_id = isset($_SESSION['identity'])? $_SESSION['identity']['id'] : '';

            if($insertId = Model_Manager::addReply($author,$author_id,$comment,$postId,$parentId)){
                $response['success']['message'] = 'Your reply has been added';
                $insertedComment = Model_Manager::getCommentById($insertId);
                $response['success']['comment'] = $insertedComment;
            }
            else{
                $response['error'] = 'An error has been occured.Please try again later';
            }

            $this->response->body = json_encode($response);


        }
    }

    public function actionDeletecomment(){
        if(Request::$isAjax){
            $this->autoRender = false;

            $commentId = $_POST['commentId'];


            if(Model_Manager::deleteComment($commentId)){
                $response['success']['message'] = 'Comment has been deleted';
            }
            else{
                $response['error'] = 'An error has been occurred.Please try again later';
            }

            $this->response->body = json_encode($response);
        }
    }
}
